import Vue from 'vue'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
// element-ui styles
import "element-ui/lib/theme-chalk/index.css"

import Login from '../views/Login'
import Register from '../views/Register'
import Admin from '../views/Admin.vue'
import AddArt from '../components/article/AddArt'
import ArtList from '../components/article/ArtList'
import Category from '../components/cate/Category'
import Comment from '../components/comment/Comment'
import AddProject from '../components/project/AddProject'
import ProjectList from '../components/project/ProjectList'
import MyEcharts from '../components/mycharts/MyEcharts'

Vue.use(ElementUI)
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/register',
    name: 'register',
    component: Register,
    meta: {
      title: "注册"
    }
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: "登录"
    }
  },
  {
    path: '/admin',
    name: 'admin',
    component: Admin,
    redirect: '/admin/artlist',
    children: [
      {
        path: 'addart',
        component: () => import("../components/article/AddArt"),
        meta: {
          title: "文章管理",
        }
      },
      {
        path: 'addart/:id',
        component: () => import("../components/article/AddArt"),
        props: true,
        meta: {
          title: "文章编辑",
        },
      },
      {
        path: 'artlist',
        component: ArtList,
        meta: {
          title: "文章列表"
        }
      },
      {
        path: 'category',
        component: Category,
        meta: {
          title: "文章分类"
        }
      },
      {
        path: 'comment',
        component: Comment,
        meta: {
          title: "文章分类"
        }
      },
      {
        path: 'addproject',
        component: AddProject,
        meta: {
          title: "添加项目"
        }
      },
      {
        path: 'addproject/:id',
        component: AddProject,
        props: true,
        meta: {
          title: "添加项目"
        }
      },
      {
        path: 'prolist',
        component: ProjectList,
        meta: {
          title: "项目列表"
        }
      },
      {
        path: 'myecharts',
        component: MyEcharts,
        meta: {
          title: "图表"
        }
      },
    ]
  }
]

// 创建路由实例
const router = new VueRouter({
  mode: 'history',
  routes
})

// 保存原来的push函数
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {

  return originalPush.call(this, location).catch(err => err)

}

/**
to 表示router即将进入的路由对象
from 表示当前即将离开的路由对象
next 通过该函数可以重新定义要跳转的路由路径。常用示例：next({ path: '/' }) 表示跳转到重定向页面
*/
// router.beforeEach((to, from, next) => {
//   if (to.meta.title) {
//     document.title = to.meta.title
//   }
//   next()

//   const userToken = window.sessionStorage.getItem('token')
//   if (to.path === '/login') return next()
//   if (!userToken) {
//     next('/login')
//   } else {
//     next()
//   }
// })

export default router;
